package com.zengqingfa.interview;

/**
 * @author zengqingfa
 * @className ABCTest2
 * @description
 * @create 2022/12/3 11:05
 */
public class ABCTest2 {

    public static Object lock = new Object();

    public static int num = 0;


    public static void main(String[] args) {
        ABCTest2 abcTest2 = new ABCTest2();
        new Thread(() -> {
            abcTest2.printABC(0);
        }, "A").start();
        new Thread(() -> {
            abcTest2.printABC(1);
        }, "B").start();
        new Thread(() -> {
            abcTest2.printABC(2);
        }, "C").start();

    }

    public void printABC(int targetNum) {
        for (int i = 0; i < 5; i++) {
            synchronized (lock) {
                while (num % 3 != targetNum) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                num++;
                System.out.println(Thread.currentThread().getName());
                lock.notifyAll();
            }
        }
    }

}